package com.runa.persistence.service;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.entity.CommNbcmd;
import com.runa.monitor.comm.pojo.NBOrder;
import com.runa.persistence.CollectionUtil;
import com.runa.persistence.StringUtil;
import com.runa.persistence.repository.CommNbcmdRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2024/12/17 13:24
 * zhangYB
 */
@Slf4j
@Service
public class CommNbCmdService {

    @Resource
    private CommNbcmdRepository commNbcmdRepository;


    public static void main(String[] args) {
        System.out.println("{}".equals(JSON.toJSONString(new NBOrder())));
    }

    public void addNbOrder(String identity, String wakeupCycle) {
        log.debug("addNbOrder wakeupCycle: {} {}", identity, wakeupCycle);
        CommNbcmd commNbcmd = commNbcmdRepository.findById(identity).orElse(null);
        if (null == commNbcmd) {
            commNbcmd = new CommNbcmd();
            commNbcmd.setNbIdentity(identity);
        }
        commNbcmd.setWakeupCycle(wakeupCycle);
        commNbcmdRepository.save(commNbcmd);
    }

    public void addNbOrder(String identity, NBOrder nbOrder) {
        log.debug("addNbOrder: {} {}", identity, JSON.toJSONString(nbOrder));
        CommNbcmd commNbcmd = commNbcmdRepository.findById(identity).orElse(null);
        if (null == commNbcmd) {
            commNbcmd = new CommNbcmd();
            commNbcmd.setNbIdentity(identity);
        }
        List<NBOrder> nbOrders = new ArrayList<>();
        if (commNbcmd.getOrderSequence() != null) {
            nbOrders = JSON.parseArray(commNbcmd.getOrderSequence(), NBOrder.class);
        }
        String wakeupCycle = nbOrder.getWakeupCycle();
        if (StringUtil.isNotBlank(wakeupCycle)) {
            commNbcmd.setWakeupCycle(wakeupCycle);
        }
        nbOrder.setWakeupCycle(null);
        if (!"{}".equals(JSON.toJSONString(nbOrder))) {
            nbOrders.add(nbOrder);
        }
        if (!nbOrders.isEmpty()) {
            commNbcmd.setOrderSequence(JSON.toJSONString(nbOrders));
        }
        commNbcmdRepository.save(commNbcmd);
    }


    public NBOrder getNBOrder(String identity) {
        CommNbcmd commNbcmd = commNbcmdRepository.findById(identity).orElse(null);
        if (null == commNbcmd) {
            return null;
        }
        List<NBOrder> nbOrders = new ArrayList<>();
        if (commNbcmd.getOrderSequence() != null) {
            nbOrders = JSON.parseArray(commNbcmd.getOrderSequence(), NBOrder.class);
        }
        String wakeupCycle = commNbcmd.getWakeupCycle();
        if (CollectionUtil.isEmpty(nbOrders)) {
            if (StringUtil.isNotBlank(wakeupCycle)) {
                NBOrder nbOrder = new NBOrder();
                nbOrder.setWakeupCycle(wakeupCycle);
                return nbOrder;
            }
        } else {
            NBOrder nbOrder = nbOrders.remove(0);
            if (nbOrders.isEmpty()) {
                commNbcmd.setOrderSequence(null);
            } else {
                commNbcmd.setOrderSequence(JSON.toJSONString(nbOrders));
            }
            commNbcmdRepository.save(commNbcmd);
            nbOrder.setWakeupCycle(wakeupCycle);
            return nbOrder;
        }
        return null;

    }

    public void clearWakeupCycle(String identity) {
        CommNbcmd commNbcmd = commNbcmdRepository.findById(identity).orElse(null);
        if (null == commNbcmd) {
            return;
        }
        commNbcmd.setWakeupCycle(null);
        commNbcmdRepository.save(commNbcmd);
    }
}
